package org.idea.spring.bean.alias;

import org.idea.spring.bean.beandefinitionbuilder.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.StringUtils;
import sun.plugin2.applet.context.NoopExecutionContext;

/**
 * @Author idea
 * @Date created in 8:46 上午 2020/6/29
 */
//通过import来导入
//@Import(BeanConfigDemo.Config.class)
public class BeanConfigDemo {

    public static void main(String[] args) {
        //这种是属于从容器里获取已经注册成功的bean组件，但是其初始化的参数需要预先定义好，不太灵活
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(BeanConfigDemo.class);
        registerUserBeanDefinition(annotationConfigApplicationContext,"idea-person");
        annotationConfigApplicationContext.refresh();
        System.out.println("=======");
        System.out.println("user class:" + annotationConfigApplicationContext.getBeansOfType(User.class));
//        System.out.println("config class:" + annotationConfigApplicationContext.getBeansOfType(Config.class));
        annotationConfigApplicationContext.close();

    }


    /**
     * 通过java的api方式来生成对应的bean
     * 这种方式生成的扩展bean比较灵活，可以在spring容器里面随意定义bean的相关信息
     *
     * @param beanDefinitionRegistry
     * @param beanName
     */
    public static void registerUserBeanDefinition(BeanDefinitionRegistry beanDefinitionRegistry,String beanName){
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        beanDefinitionBuilder
                .addPropertyValue("id",2L)
                .addPropertyValue("name","2-22idea");
        if(StringUtils.hasText(beanName)){
            beanDefinitionRegistry.registerBeanDefinition(beanName,beanDefinitionBuilder.getBeanDefinition());
        } else {
            //非命名方式注册bean
            BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionBuilder.getBeanDefinition(),beanDefinitionRegistry);
        }
    }


//    @Configuration
//    public static class Config{
//
//        @Bean(name = {"u-1","u-2"})
//        public User user(){
//            User user = new User(1,"idea");
//            return user;
//        }
//    }


//    @Bean(name = {"p-1","p-2"})
//    public Person person(){
//        Person person = new Person(1,"idea");
//        return person;
//    }

    public class Person {
        long id;
        String name;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Person(long id, String name) {
            this.id = id;
            this.name = name;
        }
    }
}
